package com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.core.util.StringUtil;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.dao.CaseSelectionExplanationDao;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.entity.*;
import com.sxfq.loansupermarket.backsystem.modular.remoteSynergy.service.ICaseSelectionExplanationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CaseSelectionExplanationServiceImpl implements ICaseSelectionExplanationService {
    @Autowired
    private CaseSelectionExplanationDao dao;

    @Autowired
    private SqlMapper sqlMapper;

    public List<ExplanationInfo> getExplanationList(Page<ExplanationInfo> page, SearchExplanationInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getLabel())) {
                params.put("label", searchInfo.getLabel());
        }
        params.put("startIndex", searchInfo.getStartIndex());
        params.put("limitIndex", searchInfo.getLimitIndex());
        return dao.getQueryPages(page, params);
    }

    public int getExplanationListCount(Page<ExplanationInfo> page, SearchExplanationInfo searchInfo) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("page", page);
        if (StringUtil.isNotEmpty(searchInfo.getLabel())) {
            params.put("label", searchInfo.getLabel());
        }
        return dao.getQueryPagesCount(page, params);
    }

    public ExplanationInfo selectById(String id) {
        String sql = "select * from `case_selection_explanation_info` where id='"+id+"' limit 1;";
        return sqlMapper.selectOne(sql,ExplanationInfo.class);
    }

    public int insert(ExplanationInfo info) {
        String sql = "INSERT INTO `case_selection_explanation_info` (`label`, `content`, `num`) VALUES" +
                "('" + info.getLabel().trim() + "','" + info.getContent().trim() + "'," + info.getNum() + ");";
        return sqlMapper.insert(sql);
    }

    @Transactional
    public int delete(String id) {
        String sql = "delete from case_selection_explanation_info where id = '" + id + "'";
        return sqlMapper.delete(sql);
    }

    public int update(ExplanationInfo info) {
        String sql = "update case_selection_explanation_info a set a.`label`='"+info.getLabel()+"', a.`content`='"+info.getContent()+"', a.`num`='"+info.getNum()+"' where id='"+info.getId()+"'";
        return sqlMapper.update(sql);
    }

}